package com.service.atf.base.model;

import com.service.atf.base.exception.TFException;
import com.service.atf.base.utils.ATFConstants;
import com.service.atf.base.utils.ATFLogger;
import com.luotao.devp.base.utils.ByteUtil;

/**
 * User: luotao
 * Date: 12-2-23
 * Time: 11:34:00
 */
public class BaseHeader {

    public static final int LEN_HEAD = 58;

    //��ݳ���
    protected short length;
    //�������1������2��ϵͳ
    protected byte type;
    //��������
    /**
     * 1��Ͷע
     * 2����ֵ
     * 3������
     * 4: �ҽ�
     * 5����½
     * 6���ǳ�
     * 7:  ע��
     * 8��������Ϸ
     * 9���˳���Ϸ
     * A�������Ϸ
     * B���˿�
     */
    protected byte tradeType;

    protected long threadid;

    protected String flowId;

    protected String customerCode;

    protected int tfCreateTime;

    public int getTfCreateTime() {
        return tfCreateTime;
    }

    public void setTfCreateTime(int tfCreateTime) {
        this.tfCreateTime = tfCreateTime;
    }

    public String getCustomerCode() {
        return customerCode;
    }

    public void setCustomerCode(String customerCode) {
        this.customerCode = customerCode;
    }

    public long getThreadid() {
        return threadid;
    }

    public void setThreadid(long threadid) {
        this.threadid = threadid;
    }

    public short getLength() {
        return length;
    }

    public void setLength(short length) {
        this.length = length;
    }

    public byte getType() {
        return type;
    }

    public void setType(byte type) {
        this.type = type;
    }

    public byte getTradeType() {
        return tradeType;
    }

    public void setTradeType(byte tradeType) {
        this.tradeType = tradeType;
    }


    public String getFlowId() {
        return flowId;
    }

    public void setFlowId(String flowId) {
        this.flowId = flowId;
    }

    public static String getTypeAndTradeType(byte[] data) {
        if (data.length < 10) {
            String msg = "��ȡTF���ʱ������ݵĽ�������Ϊ��";
            ATFLogger.syserr(msg);
            throw new TFException(ATFConstants.ATF_TF_REC_NOTYPE, msg);
        }
        StringBuffer str = new StringBuffer();

        str.append((char) data[8]).append((char) data[9]);
        return str.toString();
    }

    public void byte2ObjectHeader(byte[] src) {
        setThreadid(ByteUtil.readLong(src, 0));
        setType((byte) ByteUtil.read(src, 8));
        setTradeType((byte) ByteUtil.read(src, 9));
        setFlowId(ByteUtil.readString(src, 10, 24));
        setCustomerCode(ByteUtil.readString(src, 34, 20));
        setTfCreateTime(ByteUtil.readInt(src, 54));
    }

    public byte[] object2ByteHeader() {
        byte[] result = new byte[LEN_HEAD];
        ByteUtil.writeLong(result, 0, getThreadid());
        ByteUtil.write(result, 8, getType());
        ByteUtil.write(result, 9, getTradeType());
        ByteUtil.writeString(result, 10, getFlowId(), 24);
        ByteUtil.writeString(result, 34, getCustomerCode(), 20);
        ByteUtil.writeInt(result, 54, 10);
        return result;
    }

    public String getAreaCode() {
        return flowId.substring(2, 4);
    }

    public String getParentAreaCode() {
        return flowId.substring(0, 2);
    }

    public int getLenHead() {
        return LEN_HEAD;
    }

    //дcheckpoint
    public static byte[] getCheckPointData() {
        String fileName = Account.createACFileName();
        //  System.out.println("fileName = "+fileName);
        byte[] byte_checkpoint = new byte[64];
        ByteUtil.writeLong(byte_checkpoint, 0, 21);
        ByteUtil.write(byte_checkpoint, 8, (byte) '2');
        ByteUtil.write(byte_checkpoint, 9, (byte) '1');
        int now = 10;

        String str = "1232";
        ATFLogger.sysinfo("ADDERд��checkpoint,ʱ�� : " + str + " �ļ��� : " + fileName);
        ByteUtil.writeInt(byte_checkpoint, 10, now);
        System.arraycopy(fileName.getBytes(), 0, byte_checkpoint, 14, fileName.getBytes().length);
        return byte_checkpoint;
    }

    //дcheckpoint
    public static byte[] getTFEndData(String nextFile) {
        if (nextFile == null || "".equals(nextFile.trim())) {
            String msg = "��һ���ļ�·������Ϊ��";
            ATFLogger.syserr(msg);
            throw new RuntimeException(msg);
        }
        byte[] byte_checkpoint = new byte[64];
        ByteUtil.writeLong(byte_checkpoint, 0, 23);
        ByteUtil.write(byte_checkpoint, 8, (byte) '2');
        ByteUtil.write(byte_checkpoint, 9, (byte) '3');
        ByteUtil.writeInt(byte_checkpoint, 10, 1);
        System.arraycopy(nextFile.getBytes(), 0, byte_checkpoint, 14, nextFile.getBytes().length);
        return byte_checkpoint;
    }
}
